<?php

class BasketbalStatsTableFunctions
{

public function CheckGameStatsQuarterAvailable($season, $gamenumber)
	{

		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT * 
		FROM stats_game_quarter
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		if ( empty($row) ) {
			return false;
		} else {
			return true;
		}

	}

public function CheckGameStatsTotalsAvailable($season, $gamenumber)
	{
		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT * 
		FROM stats
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		if ( empty($row) ) {
			return false;
		} else {
			return true;
		}
	}

public function CheckGameStatsSpecificQuarterAvailable($season, $gamenumber, $team, $quarter)
	{
		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT * 
		FROM stats_game_quarter
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'
		AND quarter = '$quarter'
		AND playerID IN (SELECT users.user_key FROM users JOIN teams ON users.user_key = teams.playerID WHERE teams.teamname = '$team')
		";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		if ( empty($row) ) {

			 $sql = "SELECT * 
			FROM stats_game_quarter
			WHERE season = '$season'
			AND gamenumber = '$gamenumber'
			AND quarter = '$quarter'
			AND playerID IN (SELECT users.user_key FROM users JOIN teams_opponents ON users.user_key = teams_opponents.playerID WHERE teams_opponents.teamname = '$team')
			";

			$result = $db->query($sql);
			$row = $result->fetch_assoc(); 

			if ( empty($row) ) {
				return false;
			} else {
				return true;
			}		
		} else {
			return true;
		}
	}

public function CheckGameStatsOTAvailable($season, $gamenumber, $quarter = 'OT')
	{
		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT * 
		FROM stats_game_quarter
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'
		AND quarter = '$quarter'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		if ( empty($row) ) {

			 $sql = "SELECT * 
			FROM stats_game_quarter
			WHERE season = '$season'
			AND gamenumber = '$gamenumber'
			AND quarter = '$quarter'";

			$result = $db->query($sql);
			$row = $result->fetch_assoc(); 

			if ( empty($row) ) {
				return false;
			} else {
				return true;
			}		
		} else {
			return true;
		}
	}

public function CheckGameStatsAllQuartersAvailable($season, $gamenumber, $team)
	{
		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT quarter, count(quarter) 
		FROM stats_game_quarter 
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'
		AND playerID IN (SELECT users.user_key FROM users JOIN teams ON users.user_key = teams.playerID WHERE teams.teamname = '$team') 
		GROUP by Quarter
		";

		$result = $db->query($sql);

		if ( mysqli_num_rows($result) < 4 ) {

			$sql = "SELECT quarter, count(quarter) 
			FROM stats_game_quarter 
			WHERE season = '$season'
			AND gamenumber = '$gamenumber'
			AND playerID IN (SELECT users.user_key FROM users JOIN teams_opponents ON users.user_key = teams_opponents.playerID WHERE teams_opponents.teamname = '$team')
			GROUP by Quarter
			";

			$result = $db->query($sql);

			if ( mysqli_num_rows($result) < 4 ) {
				return false;
			} else {
				return true;
			}		
		} else {
			return true;
		}
	}

public function ShowGameStatsTableSingleQuarter($season, $gamenumber, $team, $quarter)
	{		
		if ( !$this->CheckGameStatsSpecificQuarterAvailable($season, $gamenumber, $team, $quarter) ) {
			echo "<h4> Q$quarter stats $team are not available </h4> <br/>";
		} else {
			
		require __DIR__ . '/db_bb_connect.php';

		$username = $_SESSION['username'];

		$sql = "SELECT type FROM users WHERE username = '$username'";

		$result=$db->query($sql);
		$row=$result->fetch_assoc();

		$logintype = $row['type'];

	  	$sql = "SELECT users.user_key 
	  	FROM users
	  	JOIN teams
	  	ON users.user_key = teams.playerID
	  	WHERE teams.teamname = '$team' 
	  	AND teams.season = '$season'
	  	ORDER BY teams.playernumber ASC";

	  	if ($db->query($sql)) {  
			$result = $db->query($sql);
	    } else {
	      	echo "Error: " . $sql . "<br>" . $db->error;
	    }

	    if ( mysqli_num_rows($result) == 0 ){
	    	
	    	$sql = "SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season'
		  	ORDER BY teams_opponents.playernumber ASC";

		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

	  	$playerIDs = array();

	  	$teamprint = str_replace(' ', '', $team);

	  	while ( $row = $result->fetch_assoc() ) {
	  		array_push($playerIDs,$row['user_key']);
	  	} ?>

	  	<script type="text/javascript">
			function printstats(el){
				var restorepage = document.body.innerHTML;
				var printcontent = document.getElementById(el).innerHTML;
				document.body.innerHTML = printcontent;
				window.print();
				document.body.innerHTML = restorepage;
			}
	  	</script>


	  	<div class="table-responsive statstable" id="stats<?=$teamprint?>">
	  	<h4>Stats of <?= $team; ?> Q<?= $quarter; ?> </h4><br>
	  	<button onclick="javascript:printstats('stats<?=$teamprint?>')" class="btn btn-primary btn-sm hidden-print">Print Stats</button>
	  	<table class="table table-hover table-striped">
    		<thead>
     		 	<tr>
			        <th>#</th>
			        <th colspan="2">Name</th>
			        <th>DRB</th>
			        <th>ORB</th>
			        <th>TO</th>
			        <th>ST</th>
			        <th>AST</th>
			        <th>BS</th>
			        <th>FGI</th>
			        <th>FGO</th>
			        <th>3P</th>
			        <th>FT</th>
			        <th>PTS</th>
			        <th>PF</th>
			        <th>MP</th>
			        <?php if ( $logintype == 'Coach') {
					  echo "<th>Edit</th>";
					} ?>
			    </tr>
			</thead>
			<tbody>
	  	<?php

	  	foreach ($playerIDs as $ID) {

	  		$sql = "SELECT * FROM stats_game_quarter
	  		WHERE playerID = '$ID'
	  		AND gamenumber = '$gamenumber'
	  		AND season = '$season'
	  		AND quarter = '$quarter'";

	  		if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

	    	while ( $row = $result->fetch_assoc() ) { 

	    		$playerID = $row['playerID'];

	    		$sql = "SELECT firstname, lastname
	    		FROM users
	    		WHERE user_key = '$playerID'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		$firstname = $row2['firstname'];
	    		$lastname = $row2['lastname'];

	    		$sql = "SELECT playernumber
	    		FROM teams
	    		WHERE playerID = '$playerID'
	    		AND season = '$season'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		if ( empty($row2['playernumber']) ) { 

	    			$sql = "SELECT playernumber
	    			FROM teams_opponents
	    			WHERE playerID = '$playerID'
	    			AND season = '$season'";

	    			if ($db->query($sql)) {  
						$result = $db->query($sql);
	   				} else {
	      				echo "Error: " . $sql . "<br>" . $db->error;
	    			}
	    			
	    			$row2 = $result->fetch_assoc();

	    		}		

	    		$playernumber = $row2['playernumber'];
	    		
				$DRB = $row['DRB']; 
				$ORB = $row['ORB']; 
				$TOV = $row['TOV']; 
				$ST = $row['ST']; 
				$AST = $row['AST']; 
				$BS = $row['BS']; 

				$FGIscored = $row['FGIscored']; 
				$FGItotal = $row['FGItotal'];

				$FGOscored = $row['FGOscored'];
				$FGOtotal = $row['FGOtotal'];

				$THPscored = $row['THPscored'];
				$THPtotal = $row['THPtotal'];

				$FTscored = $row['FTscored']; 
				$FTtotal = $row['FTtotal']; 

				$PF = $row['PF']; 

				$PTS = 2 * $FGOscored + 2* $FGIscored + 3 * $THPscored + $FTscored;

				$minutesplayed = floor((int)$row['MP'] / 60);
				$seconds = (int)$row['MP'] % 60;

				if ( $seconds >= 0 && $seconds < 10) {
					$secondsplayed = "0$seconds";
				} else {
					$secondsplayed = $seconds;
				}
    
				$MP = "$minutesplayed:$secondsplayed"; 

				echo "<tr>"; 

				if ($firstname == 'Team') {
					echo "<td> </td>"; 
					echo "<td> $firstname </td>";
					echo "<td> </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
				} else {					  
					echo "<td> $playernumber</td>";
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> $AST</td>"; 
					echo "<td> $BS</td>"; 
					echo "<td> $FGIscored/$FGItotal</td>";
					echo "<td> $FGOscored/$FGOtotal</td>";
					echo "<td> $THPscored/$THPtotal</td>";
					echo "<td> $FTscored/$FTtotal</td>";
					echo "<td> $PTS</td>";
					echo "<td> $PF</td>";
					echo "<td> $MP</td>";									
				}

				if ( $logintype == 'Coach') { ?>
			
						<td class="hidden-print">

						<form id="editbuttonform"> 
						<input type="hidden" name="quarter" value="<?=$quarter?>"/>
						<input type="hidden" name="playerID" value="<?=$ID?>"/>
						<input type="hidden" name="season" value="<?=$season?>"/>
						<input type="hidden" name="gamenumber" value="<?=$gamenumber?>"/>
						<button class="btn btn-sm btn-success editplayerstatsentry"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </button>
						</form>

						<form id="deletebuttonform"> 
						<input type="hidden" name="quarter" value="<?=$quarter?>"/>
						<input type="hidden" name="playerID" value="<?=$ID?>"/>
						<input type="hidden" name="season" value="<?=$season?>"/>
						<input type="hidden" name="gamenumber" value="<?=$gamenumber?>"/>
						<button class="btn btn-sm btn-danger deleteplayerstatsentry"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </button>
						</form>
						</td>

				<?php	}
				echo "</tr>";	
	    	}
	  	}

	  	$sql = "SELECT SUM(DRB) AS DRBtot, 
		SUM(ORB) AS ORBtot, 
		SUM(TOV) AS TOVtot,
		SUM(ST) AS STtot,
		SUM(AST) AS ASTtot,
		SUM(BS) AS BStot,
		SUM(FTscored) AS FTscoredtot, 
		SUM(FTtotal) AS FTtottot,
		SUM(FGIscored) AS FGIscoredtot,
		SUM(FGItotal) AS FGItot,
		SUM(FGOscored) AS FGOscoredtot,
		SUM(FGOtotal) AS FGOtot,
		SUM(THPscored) AS THPscoredtot,
		SUM(THPtotal) AS THPtot,
		SUM(MP) AS MPtot,
		SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
		SUM(PF) AS PFtot
		FROM stats_game_quarter
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'
		AND quarter = '$quarter'
		AND playerID IN (SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season')";

		
		if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

		if ( empty($row['DRBtot']) ){
	    	
		    $sql = "SELECT SUM(DRB) AS DRBtot, 
			SUM(ORB) AS ORBtot, 
			SUM(TOV) AS TOVtot,
			SUM(ST) AS STtot,
			SUM(AST) AS ASTtot,
			SUM(BS) AS BStot,
			SUM(FTscored) AS FTscoredtot, 
			SUM(FTtotal) AS FTtottot,
			SUM(FGIscored) AS FGIscoredtot,
			SUM(FGItotal) AS FGItot,
			SUM(FGOscored) AS FGOscoredtot,
			SUM(FGOtotal) AS FGOtot,
			SUM(THPscored) AS THPscoredtot,
			SUM(THPtotal) AS THPtot,
			SUM(MP) AS MPtot,
			SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
			SUM(PF) AS PFtot
			FROM stats_game_quarter
			WHERE season = '$season'
			AND gamenumber = '$gamenumber'
			AND quarter = '$quarter'
			AND playerID IN (SELECT users.user_key 
			  	FROM users
			  	JOIN teams
			  	ON users.user_key = teams.playerID
			  	WHERE teams.teamname = '$team' 
			  	AND teams.season = '$season')";


		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

	    //$row = $result->fetch_assoc();

		$DRBtot = $row['DRBtot'];
		$ORBtot = $row['ORBtot'];
		$TOVtot = $row['TOVtot'];
		$STtot = $row['STtot'];
		$ASTtot = $row['ASTtot'];
		$BStot = $row['BStot'];

		$FTscoredtot = $row['FTscoredtot'];
		$FTtottot = $row['FTtottot'];

		$FGIscoredtot = $row['FGIscoredtot'];
		$FGItot = $row['FGItot'];
		$FGOscoredtot = $row['FGOscoredtot'];
		$FGOtot = $row['FGOtot'];
		$THPscoredtot = $row['THPscoredtot'];
		$THPtot = $row['THPtot'];

		$PTStot = $row['PTStot'];
		$PFtot = $row['PFtot'];

		$minutesplayed = floor((int)$row['MPtot'] / 60);
		$seconds = (int)$row['MPtot'] % 60;

		if ( $seconds >= 0 && $seconds < 10) {
			$secondsplayed = "0$seconds";
		} else {
			$secondsplayed = $seconds;
		}

		$MPtot = "$minutesplayed:$secondsplayed"; 

		echo "<tr>";
		echo "<td> </td>";
		echo "<td><strong> TOTAL </strong> </td>";
		echo "<td></td>";
		echo "<td><strong>$DRBtot</strong></td>";
		echo "<td><strong>$ORBtot</strong></td>";		
		echo "<td><strong>$TOVtot</strong></td>";
		echo "<td><strong>$STtot</strong></td>";
		echo "<td><strong>$ASTtot</strong></td>";
		echo "<td><strong>$BStot</strong></td>";
		echo "<td><strong>$FGIscoredtot/$FGItot</strong></td>";
		echo "<td><strong>$FGOscoredtot/$FGOtot</strong></td>";
		echo "<td><strong>$THPscoredtot/$THPtot</strong></td>";
		echo "<td><strong>$FTscoredtot/$FTtottot</strong></td>";
		echo "<td><strong>$PTStot</strong></td>";
		echo "<td><strong>$PFtot</strong></td>";
		echo "<td><strong>$MPtot</strong></td>";
		echo "</tr>";	
	  	 ?>

	  		</tbody>  
		</table>
		</div>


		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

		<script type="text/javascript">
			
		$(".editplayerstatsentry").on("click", function(e) {
		    postData = $(this.form).serialize();
		    $.ajax({
		      type: "POST",
		        url: "./editplayerstats_quarter.php",
		        data: postData,
		        success: function(data) {  
		          $("#entirestatspage").html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#entirestatspage").html(xhr.responseText);
		        }
		      }); 
		    e.preventDefault();
		});


		$(".deleteplayerstatsentry").on("click", function(e) {
			postData = $(this.form).serialize();
    		var r = confirm("Are you sure to delete the entry?");
    		if (r == true) {
			    $.ajax({
			      	type: "POST",
			        url: "./includes/handlers/deleteplayerstats_quarter_handler.php",
			        data: postData,
			        success: function(data) {
			          	$("#entirestatspage").html(data);
			        },
			        error: function (xhr, status, error) {
			          $("#entirestatspage").html(xhr.responseText);
		        }
		      }); 
    		} 		    
		    e.preventDefault();
		});
		</script>
		<?php
		}
	}

public function ShowGameStatsTableFullGameFromQuarters($season, $gamenumber, $team)
	{

		if ( !$this->CheckGameStatsAllQuartersAvailable($season, $gamenumber, $team) ) {
			echo "<h4> Full game stats of $team are not available. <br> Some quarter stats are probably missing. </h4> <br/>";
		} else {

		require __DIR__ . '/db_bb_connect.php';

		$username = $_SESSION['username'];

		$sql = "SELECT user_key FROM users WHERE username = '$username'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		$user_key = $row['user_key'];

		$sql2 = "INSERT INTO clicked_items (user_key, clicked_item)
		VALUES ('$user_key', 'full_game_stats_quarter_$season"."_$gamenumber')";

		if ($db->query($sql2)) {  
    	} else {
			echo "Error: " . $sql . "<br>" . $db->error;
	    }

		$sql = "SELECT users.user_key 
	  	FROM users
	  	JOIN teams
	  	ON users.user_key = teams.playerID
	  	WHERE teams.teamname = '$team' 
	  	AND teams.season = '$season'
	  	AND teams.playertype = 'player'
	  	ORDER BY teams.playernumber ASC";

	  	if ($db->query($sql)) {  
			$result = $db->query($sql);
	    } else {
	      	echo "Error: " . $sql . "<br>" . $db->error;
	    }

	    if ( mysqli_num_rows($result) == 0 ){
	    	
	    	$sql = "SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season'
		  	AND teams_opponents.playertype = 'player'
		  	ORDER BY teams_opponents.playernumber ASC";

		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

	  	$playerIDs = array();

	  	$teamprint = str_replace(' ', '', $team);

	  	while ( $row = $result->fetch_assoc() ) {
	  		array_push($playerIDs,$row['user_key']);
	  	} ?>

	  	<link rel="stylesheet" type="text/css" href="./css/style.css">

	  	<script type="text/javascript">
			function printfullstats(el){
				var restorepage = document.body.innerHTML;
				var printcontent = document.getElementById(el).innerHTML;
				document.body.innerHTML = printcontent;
				window.print();
				document.body.innerHTML = restorepage;
			}
	  	</script>

	  	<div class="table-responsive statstable" id="stats<?=$teamprint?>">
	  	<h4>Full Game Stats of <?= $team; ?> </h4><br>
	  	<button onclick="javascript:printfullstats('stats<?=$teamprint?>')" class="btn btn-primary btn-sm hidden-print">Print Stats</button>
	  	<table class="table table-hover table-striped">
    		<thead>
     		 	<tr>
     		 		<th>#</th>
			        <th colspan="2">Name</th>
			        <th>DRB</th>
			        <th>ORB</th>
			        <th>TO</th>
			        <th>ST</th>
			        <th>AST</th>
			        <th>BS</th>
			        <th>FGI</th>
			        <th>FGO</th>
			        <th>3P</th>
			        <th>FT</th>
			        <th>PTS</th>
			        <th>PF</th>
			        <th>MP</th>
			        
			    </tr>
			</thead>
			<tbody>

	  	<?php

	  	foreach ($playerIDs as $ID) {

	  		$sql = "SELECT SUM(DRB) AS DRB, 
			SUM(ORB) AS ORB, 
			SUM(TOV) AS TOV,
			SUM(ST) AS ST,
			SUM(AST) AS AST,
			SUM(BS) AS BS,
			SUM(FTscored) AS FTscored, 
			SUM(FTtotal) AS FTtotal,
			SUM(FGIscored) AS FGIscored,
			SUM(FGItotal) AS FGItotal,
			SUM(FGOscored) AS FGOscored,
			SUM(FGOtotal) AS FGOtotal,
			SUM(THPscored) AS THPscored,
			SUM(THPtotal) AS THPtotal,
			SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTS, 
			SUM(PF) AS PF,
			SUM(MP) AS MP
	  		FROM stats_game_quarter
	  		WHERE playerID = '$ID'
	  		AND gamenumber = '$gamenumber'
	  		AND season = '$season'";

	  		if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

	    	while ( $row = $result->fetch_assoc() ) { 

	    		$sql = "SELECT firstname, lastname
	    		FROM users
	    		WHERE user_key = '$ID'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		$firstname = $row2['firstname'];
	    		$lastname = $row2['lastname'];

	    		$sql = "SELECT playernumber
	    		FROM teams
	    		WHERE playerID = '$ID'
	    		AND season = '$season'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		if ( empty($row2['playernumber']) ) { 

	    			$sql = "SELECT playernumber
	    			FROM teams_opponents
	    			WHERE playerID = '$ID'
	    			AND season = '$season'";

	    			if ($db->query($sql)) {  
						$result = $db->query($sql);
	   				} else {
	      				echo "Error: " . $sql . "<br>" . $db->error;
	    			}
	    			
	    			$row2 = $result->fetch_assoc();

	    		}		

	    		$playernumber = $row2['playernumber'];

				$DRB = $row['DRB']; 
				$ORB = $row['ORB']; 
				$TOV = $row['TOV']; 
				$ST = $row['ST']; 
				$AST = $row['AST']; 
				$BS = $row['BS']; 


				$FGIscored = $row['FGIscored']; 
				$FGItotal = $row['FGItotal'];

				$FGOscored = $row['FGOscored'];
				$FGOtotal = $row['FGOtotal'];

				$THPscored = $row['THPscored'];
				$THPtotal = $row['THPtotal'];

				$FTscored = $row['FTscored']; 
				$FTtotal = $row['FTtotal']; 

				$PF = $row['PF']; 

				$PTS = $row['PTS'];

				//$PTS = 2 * $FGOscored + 2* $FGIscored + 3 * $THPscored + $FTscored;

				$minutesplayed = floor((int)$row['MP'] / 60);
				$seconds = (int)$row['MP'] % 60;

				if ( $seconds >= 0 && $seconds < 10) {
					$secondsplayed = "0$seconds";
				} else {
					$secondsplayed = $seconds;
				}
    
				$MP = "$minutesplayed:$secondsplayed"; 

				if ( $firstname != 'Team' && empty($row['MP']) ) {
					
					echo "<tr>";  
					echo "<td> $playernumber</td>"; 
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td colspan='13'>DNP </td>"; 
					echo "</tr>";
					

				} elseif ($firstname == 'Team') {

					echo "<tr>";  
					echo "<td> </td>"; 
					echo "<td> $firstname </td>";
					echo "<td> </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "</tr>";
				} else {
					echo "<tr>";   
					echo "<td> $playernumber </td>";
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> $AST</td>"; 
					echo "<td> $BS</td>"; 
					echo "<td> $FGIscored/$FGItotal</td>";
					echo "<td> $FGOscored/$FGOtotal</td>";
					echo "<td> $THPscored/$THPtotal</td>";
					echo "<td> $FTscored/$FTtotal</td>";
					echo "<td> $PTS</td>";
					echo "<td> $PF</td>";
					echo "<td> $MP</td>";
					echo "</tr>";					
				}
	    	}
	  	}

	  	$sql = "SELECT SUM(DRB) AS DRBtot, 
		SUM(ORB) AS ORBtot, 
		SUM(TOV) AS TOVtot,
		SUM(ST) AS STtot,
		SUM(AST) AS ASTtot,
		SUM(BS) AS BStot,
		SUM(FTscored) AS FTscoredtot, 
		SUM(FTtotal) AS FTtottot,
		SUM(FGIscored) AS FGIscoredtot,
		SUM(FGItotal) AS FGItot,
		SUM(FGOscored) AS FGOscoredtot,
		SUM(FGOtotal) AS FGOtot,
		SUM(THPscored) AS THPscoredtot,
		SUM(THPtotal) AS THPtot,
		SUM(MP) AS MPtot,
		SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
		SUM(PF) AS PFtot
		FROM stats_game_quarter
		WHERE season = '$season'
		AND gamenumber = '$gamenumber'
		AND playerID IN (SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season')";

		
		if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

		if ( empty($row['DRBtot']) ){
	    	
		    $sql = "SELECT SUM(DRB) AS DRBtot, 
			SUM(ORB) AS ORBtot, 
			SUM(TOV) AS TOVtot,
			SUM(ST) AS STtot,
			SUM(AST) AS ASTtot,
			SUM(BS) AS BStot,
			SUM(FTscored) AS FTscoredtot, 
			SUM(FTtotal) AS FTtottot,
			SUM(FGIscored) AS FGIscoredtot,
			SUM(FGItotal) AS FGItot,
			SUM(FGOscored) AS FGOscoredtot,
			SUM(FGOtotal) AS FGOtot,
			SUM(THPscored) AS THPscoredtot,
			SUM(THPtotal) AS THPtot,
			SUM(MP) AS MPtot,
			SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
			SUM(PF) AS PFtot
			FROM stats_game_quarter
			WHERE season = '$season'
			AND gamenumber = '$gamenumber'
			AND playerID IN (SELECT users.user_key 
			  	FROM users
			  	JOIN teams
			  	ON users.user_key = teams.playerID
			  	WHERE teams.teamname = '$team' 
			  	AND teams.season = '$season')";


		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

		$DRBtot = $row['DRBtot'];
		$ORBtot = $row['ORBtot'];
		$TOVtot = $row['TOVtot'];
		$STtot = $row['STtot'];
		$ASTtot = $row['ASTtot'];
		$BStot = $row['BStot'];

		$FTscoredtot = $row['FTscoredtot'];
		$FTtot = $row['FTtottot'];

		$FGIscoredtot = $row['FGIscoredtot'];
		$FGItot = $row['FGItot'];
		$FGOscoredtot = $row['FGOscoredtot'];
		$FGOtot = $row['FGOtot'];
		$THPscoredtot = $row['THPscoredtot'];
		$THPtot = $row['THPtot'];

		$PTStot = $row['PTStot'];
		$PFtot = $row['PFtot'];

		$minutesplayed = floor((int)$row['MPtot'] / 60);
		$seconds = (int)$row['MPtot'] % 60;

		if ( $seconds >= 0 && $seconds < 10) {
			$secondsplayed = "0$seconds";
		} else {
			$secondsplayed = $seconds;
		}

		$MPtot = "$minutesplayed:$secondsplayed"; 


		if($FTtot != 0){
			$FTperc = round($FTscoredtot/$FTtot*100);
		} else{
			$FTperc = 0;
		}

		if($FGItot != 0){
			$FGIperc = round($FGIscoredtot/$FGItot*100);
		} else{
			$FGIperc = 0;
		}

		if($FGOtot != 0){
			$FGOperc = round($FGOscoredtot/$FGOtot*100);
		} else{
			$FGOperc = 0;
		}

		if($THPtot != 0){
			$THPperc = round($THPscoredtot/$THPtot*100);
		} else{
			$THPperc = 0;
		}


		echo "<tr>";
		echo "<td> </td>";
		echo "<td><strong> TOTAL </strong> </td>";
		echo "<td></td>";
		echo "<td><strong>$DRBtot</strong></td>";
		echo "<td><strong>$ORBtot</strong></td>";		
		echo "<td><strong>$TOVtot</strong></td>";
		echo "<td><strong>$STtot</strong></td>";
		echo "<td><strong>$ASTtot</strong></td>";
		echo "<td><strong>$BStot</strong></td>";
		echo "<td><strong>$FGIscoredtot/$FGItot ($FGIperc%)</strong></td>";
		echo "<td><strong>$FGOscoredtot/$FGOtot ($FGOperc%)</strong></td>";
		echo "<td><strong>$THPscoredtot/$THPtot ($THPperc%)</strong></td>";
		echo "<td><strong>$FTscoredtot/$FTtot ($FTperc%)</strong></td>";
		echo "<td><strong>$PTStot</strong></td>";
		echo "<td><strong>$PFtot</strong></td>";
		echo "<td><strong>$MPtot</strong></td>";
		echo "</tr>";	


	  	 ?>

	  		</tbody>  
		</table>
		</div>

		<?php
		}
		
	}

public function ShowGameStatsTableFullFromTotals($season, $gamenumber, $team)
	{
		require __DIR__ . '/db_bb_connect.php';

		$username = $_SESSION['username'];

		$sql = "SELECT type, user_key FROM users WHERE username = '$username'";

		$result = $db->query($sql);
		$row = $result->fetch_assoc();

		$user_key = $row['user_key'];

		$sql2 = "INSERT INTO clicked_items (user_key, clicked_item)
		VALUES ('$user_key', 'full_game_stats_old_$season"."_$gamenumber')";

		if ($db->query($sql2)) {  
    	} else {
			echo "Error: " . $sql . "<br>" . $db->error;
	    } 

		$logintype = $row['type'];

	  	$sql = "SELECT users.user_key 
	  	FROM users
	  	JOIN teams
	  	ON users.user_key = teams.playerID
	  	WHERE teams.teamname = '$team' 
	  	AND teams.season = '$season'
	  	ORDER BY teams.playernumber ASC";

	  	if ($db->query($sql)) {  
			$result = $db->query($sql);
	    } else {
	      	echo "Error: " . $sql . "<br>" . $db->error;
	    }

	  	$playerIDs = array();

	  	$teamprint = str_replace(' ', '', $team);

	  	while ( $row = $result->fetch_assoc() ) {
	  		array_push($playerIDs,$row['user_key']);
	  	} ?>

	  	<link rel="stylesheet" type="text/css" href="./css/style.css">

	  	<script type="text/javascript">
			function printfullstats(el){
				var restorepage = document.body.innerHTML;
				var printcontent = document.getElementById(el).innerHTML;
				document.body.innerHTML = printcontent;
				window.print();
				document.body.innerHTML = restorepage;
			}
	  	</script>

	  	<div class="table-responsive statstable" id="stats<?=$teamprint?>">
	  	<h4>Full Game Stats of <?= $team; ?> </h4><br>
	  	<!-- <button onclick="javascript:printfullstats('stats<?=$teamprint?>')" class="btn btn-primary btn-sm hidden-print">Print Stats</button> -->
	  	<table class="table table-hover table-striped">
    		<thead>
     		 	<tr>
     		 		<th>#</th>
			        <th colspan="2">Name</th>
			        <th>DRB</th>
			        <th>ORB</th>
			        <th>TO</th>
			        <th>ST</th>
					<?php 
					if ($season >= '2018/2019') { 
			        	echo "<th>AST</th>";
			        	echo "<th>BS</th>";
			        } 

					if ($season < '2017/2018') {
					  echo "<th>FG</th>";
					} else if ($season >= '2017/2018') {
					  echo "<th>FGI</th>";
					  echo "<th>FGO</th>";
					  echo "<th>3P</th>";
					}
					?> 
			        <th>FT</th>
			        <th>PTS</th>
			        <th>PF</th>
        			<?php 
        			if ($season >= '2018/2019') { 
                    echo "<th>MP</th>"; 
                    echo "<th>S5</th>"; 
                  	} 
					if ( $logintype == 'Coach') {
					  echo "<th>Edit</th>";
					}

					?>
			    </tr>
			</thead>
			<tbody>

			<?php

			$sql = "SELECT *
			FROM users
			INNER JOIN
			stats ON
			users.user_key = stats.playerID
			WHERE stats.season = '$season' AND stats.gamenumber = '$gamenumber'
			ORDER BY users.playernumber";

			$result=$db->query($sql);


			while($row=$result->fetch_assoc()){	

				$DNP = $row['DNP'];
				$type = $row['type']; 
				$firstname = $row['firstname']; 
				$lastname = $row['lastname'];
				$playerID = $row['playerID'];

				$sql = "SELECT playernumber
				FROM teams
				WHERE playerID = '$playerID'
				AND season = '$season'";

				$result2=$db->query($sql);
				$row2=$result2->fetch_assoc();

				$playernumber = $row2['playernumber'];


				if ( $type == 'Player') {
					if ( $DNP != 'no'){
						echo "<tr>";   
						echo "<td> $playernumber </td>";
						echo "<td> $firstname </td>";
						echo "<td> $lastname </td>";
						if ($DNP == 'yes'){
							if ($season < '2017/2018') {
								echo "<td colspan='8'> DNP </td>";        
							} else if ($season == '2017/2018') {
								echo "<td colspan='10'> DNP </td>";
							} else if ($season >= '2018/2019') {
								echo "<td colspan='14'> DNP </td>";
							}
						} else if ($DNP == 'injury'){
							if ($season < '2017/2018') {
								echo "<td colspan='8'> DNP Injury</td>";        
							} else if ($season == '2017/2018') {
								echo "<td colspan='10'> DNP Injury </td>";
							} else if ($season >= '2018/2019') {
								echo "<td colspan='14'> DNP </td>";
							}
						} else if ($DNP == 'coach'){
							if ($season < '2017/2018') {
								echo "<td colspan='8'> DNP Coach</td>";
							} else if ($season == '2017/2018') {
								echo "<td colspan='10'> DNP Coach </td>";
							} else if ($season >= '2018/2019') {
								echo "<td colspan='14'> DNP </td>";
						}
						}

  					if ( $logintype == 'Coach') { ?>
			
						<td class="hidden-print">

						<form id="editbuttonform"> 
						<input type="hidden" name="playerID" value="<?=$playerID?>"/>
						<input type="hidden" name="season" value="<?=$season?>"/>
						<input type="hidden" name="gamenumber" value="<?=$gamenumber?>"/>
						<button class="btn btn-sm btn-success editplayerstatsentry"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </button>
						
						</form>

						<form id="deletebuttonform"> 
						<input type="hidden" name="playerID" value="<?=$playerID?>"/>
						<input type="hidden" name="season" value="<?=$season?>"/>
						<input type="hidden" name="gamenumber" value="<?=$gamenumber?>"/>
						<button class="btn btn-sm btn-danger deleteplayerstatsentry"> <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> </button>
						</form>
						</td>

     				<?php
					}

			  		echo "</tr>";

			    	} else if ($DNP == 'no'){

					$DRB = $row['DRB']; 
					$ORB = $row['ORB']; 
					$TOV = $row['TOV']; 
					$ST = $row['ST']; 
					$AST = $row['AST']; 
					$BS = $row['BS']; 

					$FTscored = $row['FTscored']; 
					$FTtotal = $row['FTtotal']; 
					$FGscored = $row['FGscored']; 
					$FGtotal = $row['FGtotal'];

					$FG_outside_scored = $row['FG_outside_scored'];
					$FG_outside_total = $row['FG_outside_total'];

					$THPscored = $row['THPscored'];
					$THPtotal = $row['THPtotal'];

					$PF = $row['PF'];  

					$S5 = $row['S5'];

					$minutesplayed = floor((int)$row['MP'] / 60);
					$seconds = (int)$row['MP'] % 60;
    
				    if ( $seconds >= 0 && $seconds < 10) {
				      $secondsplayed = "0$seconds";
				    } else {
				        $secondsplayed = $seconds;
				    }
    
					$MP = "$minutesplayed:$secondsplayed";  

					echo "<tr>";  
			   		echo "<td> $playernumber </td>"; 
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>";

			  		if ($season >= '2018/2019') { 
			  			echo "<td> $AST</td>";
			  			echo "<td> $BS</td>"; 
			  		}
									
					echo "<td> $FGscored/$FGtotal</td>";

					if ($season >= '2017/2018') {
						echo "<td> $FG_outside_scored/$FG_outside_total</td>";
						echo "<td> $THPscored/$THPtotal</td>";
					}

					echo "<td> $FTscored/$FTtotal</td>";

					if ($season < '2017/2018') {
						$PTS = 2 * $FGscored + $FTscored;
						echo "<td>$PTS </td>";
					} else if ($season >= '2017/2018') { 
						$PTS = 2 * ($FGscored + $FG_outside_scored) + $FTscored + 3 * $THPscored;
						echo "<td>$PTS </td>";

					}

					echo "<td> $PF</td>";

					if ($season >= '2018/2019') { 
							echo "<td>$MP</td>"; 
							echo "<td>$S5</td>"; 
					                  }

					if ( $logintype == 'Coach') {
							echo "<td>
							<form id=\"editbuttonform\"> 
							<input type='hidden' name='playerID' value='$playerID'/>
							<input type='hidden' name='season' value='$season'/>
							<input type='hidden' name='gamenumber' value='$gamenumber'/>
							<button class='btn btn-sm btn-success editplayerstatsentry'> <span class='glyphicon glyphicon-pencil' aria-hidden=\"true\"></span> </button>
							</form>

							<form id=\"deletebuttonform\"> 
							<input type='hidden' name='playerID' value='$playerID'/>
							<input type='hidden' name='season' value='$season'/>
							<input type='hidden' name='gamenumber' value='$gamenumber'/>
							<button class=\"btn btn-sm btn-danger deleteplayerstatsentry\"> <span class=\"glyphicon glyphicon-trash\" aria-hidden=\"true\"></span> </button>
							</form>
							</td>";
					}

							echo "</tr>";
					  		}
						}
					}

				if ($season >= '2018/2019') { 

					$sql = "SELECT ORB, DRB, TOV, ST FROM stats WHERE gamenumber = '$gamenumber' AND season = '$season' AND playerID = 'team'";

					$result = $db->query($sql);
					$row = $result->fetch_assoc();

				  	$ORB = $row['ORB'];
				  	$DRB = $row['DRB']; 
				  	$TO = $row['TOV']; 
			      	$ST = $row['ST']; 

			        echo "<tr>"; 
			        echo "<td></td>"; 
			        echo "<td colspan='2'>Team</td>"; 
			        echo "<td>$DRB</td>"; 
			        echo "<td>$ORB</td>"; 
			        echo "<td>$TO</td>"; 
			        echo "<td>$ST</td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>"; 
			        echo "<td></td>";
			        echo "</tr>";  
				}

					$sql = "SELECT SUM(DRB) AS DRBtot, 
					SUM(ORB) AS ORBtot, 
					SUM(TOV) AS TOVtot,
					SUM(ST) AS STtot,
					SUM(AST) AS ASTtot,
					SUM(BS) AS BStot,
					SUM(FTscored) AS FTscoredtot, 
					SUM(FTtotal) AS FTtottot,
					SUM(FGscored) AS FGscoredtot,
					SUM(FGtotal) AS FGtottot,
					SUM(FG_outside_scored) AS FG_outside_scoredtot,
					SUM(FG_outside_total) AS FG_outside_totaltot,
					SUM(THPscored) AS THPscoredtot,
					SUM(THPtotal) AS THPtotaltot,
					SUM((2 * FGscored) + FTscored) AS PTStotOLD, 
					SUM( (2 * FGscored) + (2 * FG_outside_scored) + FTscored + (3 * THPscored)) AS PTStotNEW, 
					SUM(PF) AS PFtot
					FROM stats
					WHERE season = '$season'
					AND gamenumber = '$gamenumber'";

					$result=$db->query($sql);
					$row=$result->fetch_assoc();

					$DRBtot = $row['DRBtot'];
					$ORBtot = $row['ORBtot'];
					$TOVtot = $row['TOVtot'];
					$STtot = $row['STtot'];
					$ASTtot = $row['ASTtot'];
					$BStot = $row['BStot'];
					$FTscoredtot = $row['FTscoredtot'];
					$FTtottot = $row['FTtottot'];

					$FGscoredtot = $row['FGscoredtot'];
					$FGtottot = $row['FGtottot'];
					$FG_outside_scoredtot = $row['FG_outside_scoredtot'];
					$FG_outside_totaltot = $row['FG_outside_totaltot'];
					$THPscoredtot = $row['THPscoredtot'];
					$THPtotaltot = $row['THPtotaltot'];

					$PTStotOLD = $row['PTStotOLD'];
					$PTStotNEW = $row['PTStotNEW'];

					$PFtot = $row['PFtot'];

					if($FTtottot != 0){
					    $FTperc = $FTscoredtot/$FTtottot;
					  } else{
					    $FTperc = 0;
					  }

					  if($FGtottot != 0){
					    $FGperc = $FGscoredtot/$FGtottot;
					  } else{
					    $FGperc = 0;
					  }

					  if($FG_outside_totaltot != 0){
					    $FGOperc = $FG_outside_scoredtot/$FG_outside_totaltot;
					  } else{
					    $FGOperc = 0;
					  }

					  if($THPtotaltot != 0){
					    $THPperc = $THPscoredtot/$THPtotaltot;
					  } else{
					    $THPperc = 0;
					  }


					echo "<tr>";   
					echo "<td></td>"; 
					echo "<td colspan='2'> <strong> Total </strong> </td>";
					echo "<td><strong>  $DRBtot </strong></td>";
					echo "<td><strong>  $ORBtot </strong></td>";
					echo "<td><strong>  $TOVtot</strong></td>";
					echo "<td><strong>  $STtot</strong></td>";

					if ($season >= '2018/2019') {
						echo "<td><strong>  $ASTtot</strong></td>";
						echo "<td><strong>  $BStot</strong></td>";
					}


					echo "<td><strong>  $FGscoredtot/$FGtottot <br>(" . round($FGperc *100) ."%)</strong></td>";						

					if ($season >= '2017/2018') {
						echo "<td><strong>$FG_outside_scoredtot/$FG_outside_totaltot <br>(" . round($FGOperc *100) ."%)</strong></td>";
						echo "<td><strong>$THPscoredtot/$THPtotaltot <br>(" . round($THPperc *100) ."%)</strong></td>";
					}

					echo "<td><strong>  $FTscoredtot/$FTtottot <br>(" . round($FTperc *100) ."%)</strong></td>";

					if ($season < '2017/2018') {
					  	echo "<td><strong> $PTStotOLD </strong></td>";
					} else if ($season >= '2017/2018') {
						echo "<td><strong> $PTStotNEW </strong></td>";
					}

					echo "<td><strong>$PFtot</strong></td>";
						

					if ($season >= '2018/2019') {  
					    echo "<td>-</td>"; }

					if ( $logintype == 'Coach') {
					    echo "<td>-</td>";}

					echo "</tr>";
					?>	
			</tbody>
		</table>

		<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

		<script type="text/javascript">


		function myFunction() {
		    window.print();
		}
		  
		$(".editplayerstatsentry").on("click", function(e) {
		    postData = $(this.form).serialize();
		    $.ajax({
		      type: "POST",
		        url: "./editplayerstats.php",
		        data: postData,
		        success: function(data) {  
		          $("#entirestatspage").html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#entirestatspage").html(xhr.responseText);
		        }
		      }); 
		    e.preventDefault();
		});


		$(".deleteplayerstatsentry").on("click", function(e) {
		    postData = $(this.form).serialize();
		    $.ajax({
		      type: "POST",
		        url: "./includes/handlers/deleteplayerstats_handler.php",
		        data: postData,
		        success: function(data) {    
		          $("#entirestatspage").html(data);
		        },
		        error: function (xhr, status, error) {
		          $("#entirestatspage").html(xhr.responseText);
		        }
		      }); 
		    e.preventDefault();
		});

		</script>
		</div>
		<?php 
	}

public function ShowCumuGameStatsTableFromQuarters($season, $gamenumber, $team)
	{
		echo "Cumu game stats table from quarters<br>";
	}

public function ShowCumuGameStatsTableFromTotals($season, $gamenumber, $team)
	{	
		echo "Cumu game stats table from totals<br>";
	}

public function ShowCumuGameStatsTableHalfFromQuarters($season, $gamenumber, $team, $half)
	{	
		if ( $half == "firsthalf" ) {
			$tabletitle = "1st half";
			$quarter1 = '1';
			$quarter2 = '2';
		} elseif ( $half == "secondhalf" ) {
			$tabletitle = "2nd half";
			$quarter1 = '3';
			$quarter2 = '4';
		}

		if ( !$this->CheckGameStatsAllQuartersAvailable($season, $gamenumber, $team) ) {
			echo "<h4>$tabletitle stats of $team are not available </h4> <br/>";
		} else {

		require __DIR__ . '/db_bb_connect.php';

		if ( $half == "firsthalf" ) {
			$tabletitle = "1st half";
			$quarter1 = '1';
			$quarter2 = '2';
		} elseif ( $half == "secondhalf" ) {
			$tabletitle = "2nd half";
			$quarter1 = '3';
			$quarter2 = '4';
		}
		

	  	$sql = "SELECT users.user_key 
	  	FROM users
	  	JOIN teams
	  	ON users.user_key = teams.playerID
	  	WHERE teams.teamname = '$team' 
	  	AND teams.season = '$season'
	  	AND teams.playertype = 'player'
	  	ORDER BY teams.playernumber ASC";

	  	if ($db->query($sql)) {  
			$result = $db->query($sql);
	    } else {
	      	echo "Error: " . $sql . "<br>" . $db->error;
	    }

	    if ( mysqli_num_rows($result) == 0 ){
	    	
	    	$sql = "SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season'
		  	AND teams_opponents.playertype = 'player'
		  	ORDER BY teams_opponents.playernumber ASC";

		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

	  	$playerIDs = array();

	  	$teamprint = str_replace(' ', '', $team);

	  	while ( $row = $result->fetch_assoc() ) {
	  		array_push($playerIDs,$row['user_key']);
	  	} ?>

	  	<script type="text/javascript">
			function printstats(el){
				var restorepage = document.body.innerHTML;
				var printcontent = document.getElementById(el).innerHTML;
				document.body.innerHTML = printcontent;
				window.print();
				document.body.innerHTML = restorepage;
			}
	  	</script>

	  	<div class="table-responsive statstable" id="stats<?=$teamprint?>">
	  	<h4> <?= $tabletitle; ?> stats of <?= $team; ?> </h4><br>
	  	<button onclick="javascript:printstats('stats<?=$teamprint?>')" class="btn btn-primary btn-sm hidden-print">Print Stats</button>
	  	<table class="table table-hover table-striped">
    		<thead>
     		 	<tr>
			        <th>#</th>
			        <th colspan="2">Name</th>
			        <th>DRB</th>
			        <th>ORB</th>
			        <th>TO</th>
			        <th>ST</th>
			        <th>AST</th>
			        <th>BS</th>
			        <th>FGI</th>
			        <th>FGO</th>
			        <th>3P</th>
			        <th>FT</th>
			        <th>PTS</th>
			        <th>PF</th>
			        <th>MP</th>
			    </tr>
			</thead>
			<tbody>

	  	<?php

	  	foreach ($playerIDs as $ID) {

	  		// ORB DRB TOV ST BS AST FGOscored FGIscored THPscored FTscored FGOtotal FGItotal THPtota FTtotal PF MP 

	  		$sql = "SELECT SUM(ORB) AS ORB, SUM(DRB) AS DRB, SUM(TOV) AS TOV, SUM(ST) AS ST, SUM(BS) AS BS, SUM(AST) AS AST, 
	  		SUM(FGIscored) AS FGIscored, SUM(FGOscored) AS FGOscored, SUM(FTscored) AS FTscored, SUM(THPscored) AS THPscored,
	  		SUM(FGItotal) AS FGItotal, SUM(FGOtotal) AS FGOtotal, SUM(FTtotal) AS FTtotal, SUM(THPtotal) AS THPtotal,
	  		SUM(PF) AS PF, SUM(MP) AS MP
	  		FROM stats_game_quarter
	  		WHERE ( quarter = '$quarter1' OR quarter = '$quarter2' )
	  		AND playerID = '$ID'
	  		AND gamenumber = '$gamenumber'
	  		AND season = '$season'
	  		";

	  		if ($db->query($sql)) {  
				$result = $db->query($sql);
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

	    	while ( $row = $result->fetch_assoc() ) { 

	    		$sql = "SELECT firstname, lastname
	    		FROM users
	    		WHERE user_key = '$ID'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		$firstname = $row2['firstname'];
	    		$lastname = $row2['lastname'];

	    		$sql = "SELECT playernumber
	    		FROM teams
	    		WHERE playerID = '$ID'
	    		AND season = '$season'";

	    		if ($db->query($sql)) {  
					$result = $db->query($sql);
	   			} else {
	      			echo "Error: " . $sql . "<br>" . $db->error;
	    		}

	    		$row2 = $result->fetch_assoc();

	    		if ( empty($row2['playernumber']) ) { 

	    			$sql = "SELECT playernumber
	    			FROM teams_opponents
	    			WHERE playerID = '$ID'
	    			AND season = '$season'";

	    			if ($db->query($sql)) {  
						$result = $db->query($sql);
	   				} else {
	      				echo "Error: " . $sql . "<br>" . $db->error;
	    			}
	    			
	    			$row2 = $result->fetch_assoc();

	    		}		

	    		$playernumber = $row2['playernumber'];
	    		
				$DRB = $row['DRB']; 
				$ORB = $row['ORB']; 
				$TOV = $row['TOV']; 
				$ST = $row['ST']; 
				$AST = $row['AST']; 
				$BS = $row['BS']; 

				$FGIscored = $row['FGIscored']; 
				$FGItotal = $row['FGItotal'];

				$FGOscored = $row['FGOscored'];
				$FGOtotal = $row['FGOtotal'];

				$THPscored = $row['THPscored'];
				$THPtotal = $row['THPtotal'];

				$FTscored = $row['FTscored']; 
				$FTtotal = $row['FTtotal']; 

				$PF = $row['PF']; 

				$PTS = 2 * $FGOscored + 2* $FGIscored + 3 * $THPscored + $FTscored;

				$minutesplayed = floor((int)$row['MP'] / 60);
				$seconds = (int)$row['MP'] % 60;

				if ( $seconds >= 0 && $seconds < 10) {
					$secondsplayed = "0$seconds";
				} else {
					$secondsplayed = $seconds;
				}
    
				$MP = "$minutesplayed:$secondsplayed"; 

				if ( $firstname != 'Team' && empty($row['MP']) ) {
					
					echo "<tr>";  
					echo "<td> $playernumber</td>"; 
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td colspan='13'>DNP </td>"; 
					echo "</tr>";					

				} elseif ($firstname == 'Team') {

					echo "<tr>";  
					echo "<td> </td>"; 
					echo "<td> $firstname </td>";
					echo "<td> </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>"; 
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "<td> </td>";
					echo "</tr>";
				} else {
					echo "<tr>";   
					echo "<td> $playernumber</td>";
					echo "<td> $firstname </td>";
					echo "<td> $lastname </td>";
					echo "<td> $DRB </td>";
					echo "<td> $ORB</td>";
					echo "<td> $TOV</td>";
					echo "<td> $ST</td>"; 
					echo "<td> $AST</td>"; 
					echo "<td> $BS</td>"; 
					echo "<td> $FGIscored/$FGItotal</td>";
					echo "<td> $FGOscored/$FGOtotal</td>";
					echo "<td> $THPscored/$THPtotal</td>";
					echo "<td> $FTscored/$FTtotal</td>";
					echo "<td> $PTS</td>";
					echo "<td> $PF</td>";
					echo "<td> $MP</td>";
					echo "</tr>";					
				}
	    	}
	  	}

	  	$sql = "SELECT SUM(DRB) AS DRBtot, 
		SUM(ORB) AS ORBtot, 
		SUM(TOV) AS TOVtot,
		SUM(ST) AS STtot,
		SUM(AST) AS ASTtot,
		SUM(BS) AS BStot,
		SUM(FTscored) AS FTscoredtot, 
		SUM(FTtotal) AS FTtottot,
		SUM(FGIscored) AS FGIscoredtot,
		SUM(FGItotal) AS FGItot,
		SUM(FGOscored) AS FGOscoredtot,
		SUM(FGOtotal) AS FGOtot,
		SUM(THPscored) AS THPscoredtot,
		SUM(THPtotal) AS THPtot,
		SUM(MP) AS MPtot,
		SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
		SUM(PF) AS PFtot
		FROM stats_game_quarter
		WHERE ( quarter = '$quarter1' OR quarter = '$quarter2' )
		AND season = '$season'
		AND gamenumber = '$gamenumber'
		AND playerID IN (SELECT users.user_key 
		  	FROM users
		  	JOIN teams_opponents
		  	ON users.user_key = teams_opponents.playerID
		  	WHERE teams_opponents.teamname = '$team' 
		  	AND teams_opponents.season = '$season')";

		
		if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}

		if ( empty($row['DRBtot']) ){
	    	
		    $sql = "SELECT SUM(DRB) AS DRBtot, 
			SUM(ORB) AS ORBtot, 
			SUM(TOV) AS TOVtot,
			SUM(ST) AS STtot,
			SUM(AST) AS ASTtot,
			SUM(BS) AS BStot,
			SUM(FTscored) AS FTscoredtot, 
			SUM(FTtotal) AS FTtottot,
			SUM(FGIscored) AS FGIscoredtot,
			SUM(FGItotal) AS FGItot,
			SUM(FGOscored) AS FGOscoredtot,
			SUM(FGOtotal) AS FGOtot,
			SUM(THPscored) AS THPscoredtot,
			SUM(THPtotal) AS THPtot,
			SUM(MP) AS MPtot,
			SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
			SUM(PF) AS PFtot
			FROM stats_game_quarter
			WHERE ( quarter = '$quarter1' OR quarter = '$quarter2' )
			AND season = '$season'
			AND gamenumber = '$gamenumber'
			AND playerID IN (SELECT users.user_key 
			  	FROM users
			  	JOIN teams
			  	ON users.user_key = teams.playerID
			  	WHERE teams.teamname = '$team' 
			  	AND teams.season = '$season')";


		  	if ($db->query($sql)) {  
				$result = $db->query($sql);
				$row = $result->fetch_assoc();
	   		} else {
	      		echo "Error: " . $sql . "<br>" . $db->error;
	    	}
	    }

		$DRBtot = $row['DRBtot'];
		$ORBtot = $row['ORBtot'];
		$TOVtot = $row['TOVtot'];
		$STtot = $row['STtot'];
		$ASTtot = $row['ASTtot'];
		$BStot = $row['BStot'];

		$FTscoredtot = $row['FTscoredtot'];
		$FTtottot = $row['FTtottot'];

		$FGIscoredtot = $row['FGIscoredtot'];
		$FGItot = $row['FGItot'];
		$FGOscoredtot = $row['FGOscoredtot'];
		$FGOtot = $row['FGOtot'];
		$THPscoredtot = $row['THPscoredtot'];
		$THPtot = $row['THPtot'];

		$PTStot = $row['PTStot'];
		$PFtot = $row['PFtot'];

		$minutesplayed = floor((int)$row['MPtot'] / 60);
		$seconds = (int)$row['MPtot'] % 60;

		if ( $seconds >= 0 && $seconds < 10) {
			$secondsplayed = "0$seconds";
		} else {
			$secondsplayed = $seconds;
		}

		$MPtot = "$minutesplayed:$secondsplayed"; 

		if($FTtottot != 0){
			$FTperc = round($FTscoredtot/$FTtottot*100);
		} else{
			$FTperc = 0;
		}

		if($FGItot != 0){
			$FGIperc = round($FGIscoredtot/$FGItot*100);
		} else{
			$FGIperc = 0;
		}

		if($FGOtot != 0){
			$FGOperc = round($FGOscoredtot/$FGOtot*100);
		} else{
			$FGOperc = 0;
		}

		if($THPtot != 0){
			$THPperc = round($THPscoredtot/$THPtot*100);
		} else{
			$THPperc = 0;
		}


		echo "<tr>";
		echo "<td> </td>";
		echo "<td><strong> TOTAL </strong> </td>";
		echo "<td></td>";
		echo "<td><strong>$DRBtot</strong></td>";
		echo "<td><strong>$ORBtot</strong></td>";		
		echo "<td><strong>$TOVtot</strong></td>";
		echo "<td><strong>$STtot</strong></td>";
		echo "<td><strong>$ASTtot</strong></td>";
		echo "<td><strong>$BStot</strong></td>";
		echo "<td><strong>$FGIscoredtot/$FGItot ($FGIperc%) </strong></td>";
		echo "<td><strong>$FGOscoredtot/$FGOtot ($FGOperc%)</strong></td>";
		echo "<td><strong>$THPscoredtot/$THPtot ($THPperc%)</strong></td>";
		echo "<td><strong>$FTscoredtot/$FTtottot ($FTperc%)</strong></td>";
		echo "<td><strong>$PTStot</strong></td>";
		echo "<td><strong>$PFtot</strong></td>";
		echo "<td><strong>$MPtot</strong></td>";
		echo "</tr>";	


	  	 ?>

	  		</tbody>  
		</table>
		</div>

		<?php
		}
	}

public function ShowCumuGameStatsTableColumnsFromQuarters($season, $gamenumber, $team)
	{	
		require __DIR__ . '/db_bb_connect.php';

		$sql = "SELECT SUM(DRB) AS DRBtot, 
	    SUM(ORB) AS ORBtot, 
	    SUM(TOV) AS TOVtot,
	    SUM(ST) AS STtot,
	    SUM(AST) AS ASTtot,
	    SUM(BS) AS BStot,
	    SUM(FTscored) AS FTscoredtot, 
	    SUM(FTtotal) AS FTtottot,
	    SUM(FGIscored) AS FGIscoredtot,
	    SUM(FGItotal) AS FGItot,
	    SUM(FGOscored) AS FGOscoredtot,
	    SUM(FGOtotal) AS FGOtot,
	    SUM(THPscored) AS THPscoredtot,
	    SUM(THPtotal) AS THPtot,
	    SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
	    SUM(PF) AS PFtot
	    FROM stats_game_quarter
	    WHERE season = '$season'
	    AND gamenumber = '$gamenumber'
	    AND playerID IN (SELECT users.user_key 
	        FROM users
	        JOIN teams
	        ON users.user_key = teams.playerID
	        WHERE teams.teamname = '$team' 
	        AND teams.season = '$season')";

        $result = $db->query($sql);
        $row = $result->fetch_assoc();

        if ( empty($row['DRBtot']) ) {

	        $sql = "SELECT SUM(DRB) AS DRBtot, 
		    SUM(ORB) AS ORBtot, 
		    SUM(TOV) AS TOVtot,
		    SUM(ST) AS STtot,
		    SUM(AST) AS ASTtot,
		    SUM(BS) AS BStot,
		    SUM(FTscored) AS FTscoredtot, 
		    SUM(FTtotal) AS FTtottot,
		    SUM(FGIscored) AS FGIscoredtot,
		    SUM(FGItotal) AS FGItot,
		    SUM(FGOscored) AS FGOscoredtot,
		    SUM(FGOtotal) AS FGOtot,
		    SUM(THPscored) AS THPscoredtot,
		    SUM(THPtotal) AS THPtot,
		    SUM( (2 * FGIscored) + (2 * FGOscored) + FTscored + (3 * THPscored)) AS PTStot, 
		    SUM(PF) AS PFtot
		    FROM stats_game_quarter
		    WHERE season = '$season'
		    AND gamenumber = '$gamenumber'
		    AND playerID IN (SELECT users.user_key 
		        FROM users
		        JOIN teams_opponents
		        ON users.user_key = teams_opponents.playerID
		        WHERE teams_opponents.teamname = '$team' 
		        AND teams_opponents.season = '$season')";

		  	$result = $db->query($sql);
		  	$row = $result->fetch_assoc();
          }

          $DRBtot = $row['DRBtot'];
          $ORBtot = $row['ORBtot'];
          $TOVtot = $row['TOVtot'];
          $STtot = $row['STtot'];
          $ASTtot = $row['ASTtot'];
          $BStot = $row['BStot'];
          $FTscoredtot = $row['FTscoredtot'];
          $FTtottot = $row['FTtottot'];

          $FGscoredtot = $row['FGIscoredtot'];
          $FGtottot = $row['FGItot'];
          $FG_outside_scoredtot = $row['FGOscoredtot'];
          $FG_outside_totaltot = $row['FGOtot'];
          $THPscoredtot = $row['THPscoredtot'];
          $THPtotaltot = $row['THPtot'];

          $PTStotOLD = $row['PTStot'];
          $PTStotNEW = $row['PTStot'];

          $PFtot = $row['PFtot'];

          if($FTtottot != 0){
              $FTperc = $FTscoredtot/$FTtottot;
            } else{
              $FTperc = 0;
            }

            if($FGtottot != 0){
              $FGperc = $FGscoredtot/$FGtottot;
            } else{
              $FGperc = 0;
            }

            if($FG_outside_totaltot != 0){
              $FGOperc = $FG_outside_scoredtot/$FG_outside_totaltot;
            } else{
              $FGOperc = 0;
            }

            if($THPtotaltot != 0){
              $THPperc = $THPscoredtot/$THPtotaltot;
            } else{
              $THPperc = 0;
            }

          echo "<td>$DRBtot</td>";
          echo "<td>$ORBtot</td>";
          echo "<td>$TOVtot</td>";
          echo "<td>$STtot</td>";

          if ($season >= '2018/2019') {
            echo "<td>$ASTtot</td>";
            echo "<td>$BStot</td>";
          }


          echo "<td>$FGscoredtot/$FGtottot <br>(" . round($FGperc *100) ."%)</td>";            

          if ($season >= '2017/2018') {
            echo "<td>$FG_outside_scoredtot/$FG_outside_totaltot <br>(" . round($FGOperc *100) ."%)</td>";
            echo "<td>$THPscoredtot/$THPtotaltot <br>(" . round($THPperc *100) ."%)</td>";
          }

          echo "<td>$FTscoredtot/$FTtottot <br>(" . round($FTperc *100) ."%)</td>";

          if ($season < '2017/2018') {
              echo "<td>$PTStotOLD</td>";
          } else if ($season >= '2017/2018') {
            echo "<td>$PTStotNEW</td>";
          }

          echo "<td>$PFtot</td>";
            
	}

public function ShowCumuGameStatsTableColumnsFromTotals($season, $gamenumber)
	{
		require __DIR__ . '/db_bb_connect.php';

    	$sql = "SELECT SUM(DRB) AS DRBtot, 
				SUM(ORB) AS ORBtot, 
				SUM(TOV) AS TOVtot,
				SUM(ST) AS STtot,
				SUM(AST) AS ASTtot,
				SUM(BS) AS BStot,
				SUM(FTscored) AS FTscoredtot, 
				SUM(FTtotal) AS FTtottot,
				SUM(FGscored) AS FGIscoredtot,
				SUM(FGtotal) AS FGItot,
				SUM(FG_outside_scored) AS FGOscoredtot,
				SUM(FG_outside_total) AS FGOtot,
				SUM(THPscored) AS THPscoredtot,
				SUM(THPtotal) AS THPtotaltot,
				SUM( (2 * FGscored) + (2 * FG_outside_scored) + FTscored + (3 * THPscored)) AS PTStot, 
				SUM(PF) AS PFtot
				FROM stats
				WHERE season = '$season'
				AND gamenumber = '$gamenumber'";
        
        $result = $db->query($sql);
		$row = $result->fetch_assoc();

		$DRBtot = $row['DRBtot'];
		$ORBtot = $row['ORBtot'];
		$TOVtot = $row['TOVtot'];
		$STtot = $row['STtot'];
		$ASTtot = $row['ASTtot'];
		$BStot = $row['BStot'];
		$FTscoredtot = $row['FTscoredtot'];
		$FTtottot = $row['FTtottot'];

		$FGscoredtot = $row['FGIscoredtot'];
		$FGtottot = $row['FGItot'];
		$FG_outside_scoredtot = $row['FGOscoredtot'];
		$FG_outside_totaltot = $row['FGOtot'];
		$THPscoredtot = $row['THPscoredtot'];
		$THPtotaltot = $row['THPtotaltot'];

		$PTStotOLD = $row['PTStot'];
		$PTStotNEW = $row['PTStot'];

		$PFtot = $row['PFtot'];

		if($FTtottot != 0){
		$FTperc = $FTscoredtot/$FTtottot;
		} else{
		$FTperc = 0;
		}

		if($FGtottot != 0){
		$FGperc = $FGscoredtot/$FGtottot;
		} else{
		$FGperc = 0;
		}

		if($FG_outside_totaltot != 0){
		$FGOperc = $FG_outside_scoredtot/$FG_outside_totaltot;
		} else{
		$FGOperc = 0;
		}

		if($THPtotaltot != 0){
		$THPperc = $THPscoredtot/$THPtotaltot;
		} else{
		$THPperc = 0;
		}

		echo "<td>$DRBtot</td>";
		echo "<td>$ORBtot</td>";
		echo "<td>$TOVtot</td>";
		echo "<td>$STtot</td>";

		if ($season >= '2018/2019') {
		echo "<td>$ASTtot</td>";
		echo "<td>$BStot</td>";
		}


		echo "<td>$FGscoredtot/$FGtottot <br>(" . round($FGperc *100) ."%)</td>";            

		if ($season >= '2017/2018') {
		echo "<td>$FG_outside_scoredtot/$FG_outside_totaltot <br>(" . round($FGOperc *100) ."%)</td>";
		echo "<td>$THPscoredtot/$THPtotaltot <br>(" . round($THPperc *100) ."%)</td>";
		}

		echo "<td>$FTscoredtot/$FTtottot <br>(" . round($FTperc *100) ."%)</td>";

		if ($season < '2017/2018') {
		echo "<td>$PTStotOLD</td>";
		} else if ($season >= '2017/2018') {
		echo "<td>$PTStotNEW</td>";
		}

		echo "<td>$PFtot</td>";
    }
}

?>